<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Service\FlyBusinessDistrictService;
use App\Service\FlyShopFansService;
use Hyperf\DbConnection\Db;
use Hyperf\Validation\Rule;
use App\Annotation\LogAnnotation;
use Hyperf\Di\Annotation\Inject;

class BusinessDistrictController extends  AbstractController
{

    /**
     * @Inject
     * @var FlyBusinessDistrictService
     */
    private $businessDistrictService;

    /**
     * @Inject
     * @var FlyShopFansService
     */
    private $fans;
    /**
     * 商圈列表
     * @return array|object
     */
    public function list()
    {
        $request_p =  $this->request->inputs(['key_word','lat','lng','size','current']);
        $this->validation($request_p,
            [
                'lng'=> 'required|numeric|min:0|max:180',
                'lat'=> 'required|numeric|min:-90|max:90',
                'key_word'=> 'string|max:100',
            ],
        );
        $list  = $this->businessDistrictService->list($request_p);
        return $this->jsonResponse->success($this->paginate($list),'附近商圈列表');
    }


    /**
     * 商圈详情
     * @return array|object
     */
    public function detail()
    {
        $request_p =  $this->request->inputs(['id','lat','lng']);
        $this->validation($request_p,
            [
                'lng'=> 'required|numeric|min:0|max:180',
                'lat'=> 'required|numeric|min:-90|max:90',
                'id'=> 'required|integer|min:0',
            ],
            );
        $info  = $this->businessDistrictService->info(intval($request_p['id']),floatval($request_p['lng']),floatval($request_p['lat']));
        return $this->jsonResponse->success($info,'附近商圈详情');
    }

    /**
     * 商圈店铺列表
     * @return array|object
     */
    public function shop_list()
    {
        $request_p =  $this->request->inputs(['key_word','lat','lng','size','current','district_id','order_by','distance_meter','service','start_time','end_time']);
        $this->validation($request_p,
            [
                'lng'=> 'required|numeric|min:0|max:180',
                'lat'=> 'required|numeric|min:-90|max:90',
                'key_word'=> 'string|max:100',
                "district_id"=>'min:1|integer|exists:fly_business_district,id',
                "order_by"=>'required|min:1|max:3',
                "distance_meter"=>'min:1|max:20',
                "service"=>'array',
                "service.*"=>'min:1|max:2',
                "start_time"=>'min:0|max:2400',
                "end_time"=>'min:0|max:2400',
            ],
        );
        $list =  $this->businessDistrictService->shop_list($request_p);
        $list =  $this->paginate($list)->toArray();
        if(isset($request_p['district_id'])&&$request_p['district_id']!=''){
            $list['district_info'] = $this->businessDistrictService->info(intval($request_p['district_id']));
        }else{
            $list['district_info'] = [];
        }
        if($list['data']!=[]){
            foreach ($list['data'] as $key=>$v){
                $list['data'][$key]['shop_fans'] = $this->fans->getShopFans(intval($v['id']));
            }
        }
        return $this->jsonResponse->success($list,'商圈店铺列表');
    }
}
